package com.enzoism.springboot.dive.service;

import com.enzoism.springboot.dive.dao.MenuCustOrderDao;
import com.enzoism.springboot.dive.model.MenuCustOrder;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class MenuCustOrderService {
    @Autowired
    private MenuCustOrderDao menuCustOrderDao;

    /**
     * 查询用户自定义菜单列表
     */
    public List<MenuCustOrder> getUserCustomerMenuList(){
        // 查询系统展示的目录列表
        List<MenuCustOrder> allMenuList = menuCustOrderDao.selectAllSystemMenuList("Y");
        List<MenuCustOrder> userCustomerList = menuCustOrderDao.selectAllCustomerMenuList("xiaoming");

        //  用【用户自定义数据】替换【系统配置数据】
        if (CollectionUtils.isNotEmpty(userCustomerList)){
            Map<Integer, MenuCustOrder> usrCustomerMap = userCustomerList.stream().collect(Collectors.toMap(MenuCustOrder::getMenuId, Function.identity()));
            Set<Integer> usrCustomerMapIdSet = usrCustomerMap.keySet();
            Integer menuId;
            MenuCustOrder menuCustOrder;
            for (int i = 0; i < allMenuList.size(); i++) {
                menuCustOrder = allMenuList.get(i);
                menuId = menuCustOrder.getMenuId();
                if (CollectionUtils.containsAny(usrCustomerMapIdSet,menuId)){
                    menuCustOrder.setMenuOrder(usrCustomerMap.get(menuId).getMenuOrder());
                    menuCustOrder.setMenuShow(usrCustomerMap.get(menuId).getMenuShow());
                }
            }
        }
        // TODO 将List转化为树状结构进行返回即可
        return allMenuList;
    }
}
